package com.littlebee.storysuper.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.littlebee.storysuper.R;
import com.littlebee.storysuper.entry.StoryEntry;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.GsonUtils;
import com.littlebee.storysuper.utils.LogUtils;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.Utils;
import com.littlebee.storysuper.widget.CircleImageView;
import com.zss.library.activity.BaseActivity;
import com.zss.library.adapter.CommonAdapter;
import com.zss.library.adapter.ViewHolder;
import com.zss.library.toolbar.CommonToolbar;
import com.zss.library.utils.SharedPrefUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 今日故事
 */
public class TodayStoryActivity extends BaseActivity {

    private GridView gridView;
    private CommonAdapter<StoryEntry> mAdapter;
    List<StoryEntry> names = new ArrayList<StoryEntry>();

    @Override
    public int getLayoutResId() {
        return R.layout.activity_today_story;
    }

    @Override
    public void initView() {
        super.initView();
        gridView = (GridView) findViewById(R.id.gv);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        Map<String, String> hashMap = new HashMap<>();
        User user = Utils.getUserEntry();
        hashMap.put("phoneId", user.getMobile());
        hashMap.put("pageNum", "0");
        hashMap.put("pageSize", "500");
        loadData(hashMap);
        mAdapter = new CommonAdapter<StoryEntry>(getActivity(), R.layout.layout_item_story) {
            @Override
            protected void convert(ViewHolder viewHolder, final StoryEntry workerListEntry, int i) {
                TextView name = viewHolder.findViewById(R.id.tv);
                CircleImageView iv = viewHolder.findViewById(R.id.photo);

                name.setText(workerListEntry.getCoursename());
                // TODO: 2017/8/15
                //Glide.with(TodayStoryActivity.this).load(workerListEntry.getAudiolist().get(0).getPieceurl()).error(R.mipmap.logo_img).into(iv);


            }
        };
        gridView.setAdapter(mAdapter);
        mAdapter.addAll(names);


    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        setStatusBarColor(R.color.module_video_bg);
        CommonToolbar toolbar = getToolbar();
        toolbar.setBgRes(R.color.module_video_bg);
        toolbar.setTitle(getString(R.string.today_style_txt));

    }

    /*
   * http请求的调用汇总
   * */
    public void loadData(Map<String, String> hashParams) {
        // TODO: 2017/8/14
        NetWorkUtils.get(getActivity(), "gethistoryorder_page", hashParams,true, new NetWorkUtils.IListener() {

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                LogUtils.i("XHX", "XHXSUCC:" + result);
                try {
                    String res = resObj.getJSONObject("response").optString("data");
                    names = GsonUtils.getListFromJSON(res, StoryEntry.class);
                    LogUtils.i("XHX", "XHXSIZE:" + names.size());
                    mAdapter.replaceAll(names);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX", "XHXERROR:" + result);


            }
        });
    }
}
